import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";
import Home from "../components/Home";
import Welcome from "../components/Welcome";
import users from "../components/User/user";
import Rights from "../components/power/Rights";
import Roles from "../components/power/Roles";
import Cate from "../components/goods/Cate";
import Params from "../components/goods/Params";

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:'/login'
  },
  {
    path: "/login",
    component:Login
  },
  {
    path: "/home",
    component: Home,
    //登录成功后直接重定向到welcome页面  可以在home页面设置router-view显示入口 显示入口在哪 重定向的页面就在哪显示
    redirect: "/Welcome",
    children:[
      {path:"/Welcome",component:Welcome},
      {path: "/users",component: users},
      {path:"/rights",component: Rights},
      {path:"/roles",component: Roles},
      {path:"/categories",component: Cate},
      {path:"/params",component: Params}
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next)=>{
//  to 要访问的路径
//  from 从哪个路径来
//  next放行 next('/login')强制跳转
//  如果是登陆页面直接放行
  if (to.path==='/login') return  next();
  //获取token的值
  const tokenStr = window.sessionStorage.getItem('token');
  //判断token是否存在 如果不存在直接返回login页面
  if(!tokenStr) return next('/login');
  //存在放行
  next();

})

export default router
